﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmsUI.DataBase;
using SmsUI.DataBase.smsdbDataSetTableAdapters;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.ComponentModel;
using System.Threading;
using org.smslib;
using SmsUI.N77;

namespace SmsUI
{
    public class DbRules
    {
        public static DbRules Instance = new DbRules();
        smsdbDataSet.tb_SetDataTable setTable = null;
        smsdbDataSet.tb_ContentDataTable contentTable = null;
        smsdbDataSet.tb_SendListDataTable sendListTable = null;
        smsdbDataSet.tb_ReciveListDataTable reciveListTable = null;

        smsdbDataSet.tb_SendedListDataTable sendedListTable = null;
        smsdbDataSet.tb_ConfigDataTable configTable = null;
        smsdbDataSet.tb_CheckListDataTable UnCheckTable = null;
        smsdbDataSet.tb_CheckListDataTable CheckTable = null;
        List<string> UnCheckedNumList = null;

        public List<string> GetUnCheckedNumList()
        {
            if (UnCheckedNumList == null)
            {
                UnCheckedNumList = new List<string>();
            }
            return UnCheckedNumList;
        }
        public void UpdateSendedState(OutboundMessage om)
        {

        }

        public void InitDbRules()
        {
            DeleteTempNumber();
            GetSendListTable();
            GetCheckedListTable();
            GetConfigInfo();
            GetContentTable();
            GetReciveListTable();
            GetSendedListTable();
            GetSetInfo();

        }
        public string GetSetInfoMaType(string comPort)
        {
            smsdbDataSet.tb_SetDataTable tb = GetSetInfo();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                if (tb[i].PortNum == comPort)
                {
                    return tb[i].MaType;
                }
            }
            return "";
        }
        public static object setInfoLock = new object();
        public smsdbDataSet.tb_SetDataTable GetSetInfo()
        {
            //smsdbDataSet.tb_SetDataTable tb = new smsdbDataSet.tb_SetDataTable();
            lock (setInfoLock)
            {
                if (setTable == null)
                {


                    //using (QueriesTableAdapter adt = new QueriesTableAdapter())
                    //{
                    //    int a = adt.UpdateSetQuery();
                    //}
                    setTable = new smsdbDataSet.tb_SetDataTable();

                    using (tb_SetTableAdapter adt = new tb_SetTableAdapter())
                    {
                        adt.Fill(setTable);

                    }

                }
            }
            int count = 0;
            for (int i = 0; i < setTable.Count; i++)
            {
                if (setTable[i].State == "启用")
                {
                    count++;
                }
                if (count > CheckDog.PortCount)
                {
                    setTable[i].State = "停用";
                }
            }
            return setTable;
        }

        public void InitSetInfo()
        {
            for (int i = 0; i < setTable.Rows.Count; i++)
            {
                setTable[i].State = "停用";
            }
            setTable.AcceptChanges();
        }
        public smsdbDataSet.tb_ContentDataTable GetContentTable()
        {
            //smsdbDataSet.tb_SetDataTable tb = new smsdbDataSet.tb_SetDataTable();
            if (contentTable == null)
            {
                using (QueriesTableAdapter adt = new QueriesTableAdapter())
                {
                    int a = adt.InsertContent();
                }

                contentTable = new smsdbDataSet.tb_ContentDataTable();

                using (tb_ContentTableAdapter adt = new tb_ContentTableAdapter())
                {
                    adt.Fill(contentTable);

                }
                contentTable[contentTable.Rows.Count - 1].Delete();
                UpdateContent();
                contentTable.AcceptChanges();
            }

            return contentTable;
        }
        public int UpdateContent()
        {
            int rowCount = 0;
            using (tb_ContentTableAdapter adt = new tb_ContentTableAdapter())
            {
                rowCount = adt.Update(contentTable);

            }
            return rowCount;
        }
        public int UpdateSetInfo()
        {
            int rowCount = 0;
            using (tb_SetTableAdapter adt = new tb_SetTableAdapter())
            {
                rowCount = adt.Update(setTable);

            }
            return rowCount;
        }
        public void DeleteTempNumber()
        {
            using (QueriesTableAdapter adt = new QueriesTableAdapter())
            {
                adt.DeleteTempNumber();
            }
        }
        public smsdbDataSet.tb_SendListDataTable GetSendListTable()
        {
            //smsdbDataSet.tb_SetDataTable tb = new smsdbDataSet.tb_SetDataTable();

            if (sendListTable == null)
            {
                using (QueriesTableAdapter adt = new QueriesTableAdapter())
                {
                    int a = adt.InsertSendListNew();
                }

                sendListTable = new smsdbDataSet.tb_SendListDataTable();

                using (tb_SendListTableAdapter adt = new tb_SendListTableAdapter())
                {
                    adt.Fill(sendListTable);

                }
                sendListTable[sendListTable.Rows.Count - 1].Delete();

                UpdateSendList();
                sendListTable.AcceptChanges();

            }

            return sendListTable;
        }

        public smsdbDataSet.tb_SendListDataTable GetNeedSendInfo()
        {
            smsdbDataSet.tb_SendListDataTable sendListTable = null;

            using (tb_SendListTableAdapter adt = new tb_SendListTableAdapter())
            {
                sendListTable = adt.GetDataByID();
            }
            return sendListTable;
        }

        private static object updateSendListLock = new object();

        public int UpdateSendList()
        {
            //if (upSendListWork == null)
            //{
            //    upSendListWork = new BackgroundWorker();
            //    upSendListWork.ProgressChanged += new ProgressChangedEventHandler(upSendListWork_ProgressChanged);
            //    upSendListWork.DoWork += new DoWorkEventHandler(upSendListWork_DoWork);
            //    upSendListWork.RunWorkerCompleted += new RunWorkerCompletedEventHandler(upSendListWork_RunWorkerCompleted);
            //}
            //upSendListWork.RunWorkerAsync();
            //FormManager.Instance.ProcessForm = new ProcessingForm();
            //FormManager.Instance.ProcessForm.SetText("正在更新发送列表，请稍后...");
            //FormManager.Instance.ProcessForm.ShowDialog();
            //return 0;
            lock (updateSendListLock)
            {
                int rowCount = 0;

                using (tb_SendListTableAdapter adt = new tb_SendListTableAdapter())
                {
                    try
                    {
                        rowCount = adt.Update(sendListTable);
                    }
                    catch (Exception ex)
                    {
                        Logger.Write(ex);
                        rowCount = 0;
                    }
                }
                if (sendListTable != null)
                {
                    sendListTable.AcceptChanges();
                }
                return rowCount;

            }

        }

        void upSendListWork_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            sendListTable.AcceptChanges();
            FormManager.Instance.ProcessForm.Close();
        }

        void upSendListWork_DoWork(object sender, DoWorkEventArgs e)
        {


        }

        void upSendListWork_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            throw new NotImplementedException();
        }


        public int DeleteAllSendList()
        {
            int rowCount = 0;
            using (QueriesTableAdapter adt = new QueriesTableAdapter())
            {
                rowCount = adt.DeleteSendList();


            }
            using (QueriesTableAdapter adt = new QueriesTableAdapter())
            {
                int a = adt.InsertSendListNew();
            }

            sendListTable = new smsdbDataSet.tb_SendListDataTable();

            using (tb_SendListTableAdapter adt = new tb_SendListTableAdapter())
            {
                adt.Fill(sendListTable);

            }
            sendListTable[sendListTable.Rows.Count - 1].Delete();
            UpdateSendList();
            sendListTable.AcceptChanges();
            return rowCount;
        }
        public bool DeleteSendListByID(string id)
        {
            int rowCount = 0;
            using (QueriesTableAdapter qta = new QueriesTableAdapter())
            {
                rowCount = qta.DeleteSendListByID(int.Parse(id));
            }
            if (rowCount > 0)
            {
                return true;
            }
            return false;
        }
        public smsdbDataSet.tb_ReciveListDataTable GetReciveListTable()
        {
            //smsdbDataSet.tb_SetDataTable tb = new smsdbDataSet.tb_SetDataTable();
            if (reciveListTable == null)
            {
                using (QueriesTableAdapter adt = new QueriesTableAdapter())
                {
                    int a = adt.InsertReciveList();
                }
                reciveListTable = new smsdbDataSet.tb_ReciveListDataTable();

                using (tb_ReciveListTableAdapter adt = new tb_ReciveListTableAdapter())
                {
                    adt.Fill(reciveListTable);

                }
                reciveListTable[reciveListTable.Rows.Count - 1].Delete();
                UpdateReciveList();
                reciveListTable.AcceptChanges();
            }

            return reciveListTable;
        }
        public int UpdateReciveList()
        {
            int rowCount = 0;
            using (tb_ReciveListTableAdapter adt = new tb_ReciveListTableAdapter())
            {
                rowCount = adt.Update(reciveListTable);

            }
            return rowCount;
        }

        public smsdbDataSet.tb_SendedListDataTable GetSendedListTable()
        {
            //smsdbDataSet.tb_SetDataTable tb = new smsdbDataSet.tb_SetDataTable();
            if (sendedListTable == null)
            {
                using (QueriesTableAdapter adt = new QueriesTableAdapter())
                {
                    int a = adt.InsertSendedList();
                }
                sendedListTable = new smsdbDataSet.tb_SendedListDataTable();

                using (tb_SendedListTableAdapter adt = new tb_SendedListTableAdapter())
                {
                    adt.Fill(sendedListTable);

                }
                sendedListTable[sendedListTable.Rows.Count - 1].Delete();
               // sendedListTable[0].Delete();
                UpdateSendedList();
                sendedListTable.AcceptChanges();
            }

            return sendedListTable;
        }

        public smsdbDataSet.tb_SendedListRow GetLastSpeedCountRowInfo(int lastSpeedCount, string comPort)
        {
            int count = 0;
            smsdbDataSet.tb_SendedListRow row = null;
            if (sendedListTable.Rows.Count > 0)
            {
                for (int i = sendedListTable.Rows.Count - 1; i > -1; i--)
                {
                    if (sendedListTable[i].RowState != DataRowState.Deleted)
                    {
                        if (sendedListTable[i].SendSet == comPort)
                        {
                            count++;

                            if (count >= lastSpeedCount)
                            {
                                return sendedListTable[i];
                            }
                        }
                    }
                }
            }
            return null;
        }
        public int UpdateSendedList()
        {
            int rowCount = 0;
            using (tb_SendedListTableAdapter adt = new tb_SendedListTableAdapter())
            {
                rowCount = adt.Update(sendedListTable);


            }
            return rowCount;
        }

        public int UpdateAllSendStates()
        {
            int rowCount = 0;
            using (QueriesTableAdapter adt = new QueriesTableAdapter())
            {
                rowCount = adt.UpdateAllSendState();


            }
            return rowCount;
        }
        public int GetStartPortCount()
        {
            int count = 0;
            for (int i = 0; i < setTable.Count; i++)
            {
                if (setTable[i].State == "启用")
                {
                    count++;
                }

            }
            return count;
        }
        public int UpdateSendStates(int id, string state)
        {
            int rowCount = 0;
            using (QueriesTableAdapter adt = new QueriesTableAdapter())
            {
                rowCount = adt.UpdateSendState(state, id);


            }
            return rowCount;
        }
        public smsdbDataSet.tb_ConfigDataTable GetConfigInfo()
        {
            if (configTable == null)
            {
                configTable = new smsdbDataSet.tb_ConfigDataTable();

                using (tb_ConfigTableAdapter adt = new tb_ConfigTableAdapter())
                {
                    adt.Fill(configTable);

                }
            }
            return configTable;
        }
        public int UpdateConfigInfo()
        {
            int rowCount = 0;
            using (tb_ConfigTableAdapter adt = new tb_ConfigTableAdapter())
            {
                rowCount = adt.Update(configTable);
            }
            return rowCount;
        }

        //public void SetSendingCount(string port, int count)
        //{
        //    foreach (smsdbDataSet.tb_SetRow row in setTable.Rows)
        //    {
        //        if (row.PortNum == port)
        //        {
        //            row.SendingCount = count;

        //            break;
        //        }
        //    }
        //}
        //public int GetSendingCount(string port)
        //{

        //    foreach (smsdbDataSet.tb_SetRow row in setTable.Rows)
        //    {
        //        if (row.PortNum == port)
        //        {
        //            //   row.SendingCount = count;

        //            return row.SendingCount;
        //        }
        //    }
        //    return 0;
        //}
        //public void ClearSendingCount()
        //{
        //    foreach (smsdbDataSet.tb_SetRow row in setTable.Rows)
        //    {

        //        row.SendingCount = 0;

        //    }
        //    setTable.AcceptChanges();
        //}

        public smsdbDataSet.tb_CheckListDataTable GetUnCheckedListTable()
        {
            //smsdbDataSet.tb_SetDataTable tb = new smsdbDataSet.tb_SetDataTable();
            if (UnCheckTable == null)
            {
                using (QueriesTableAdapter adt = new QueriesTableAdapter())
                {
                    int a = adt.InsertUnCheckList();
                }
                UnCheckTable = new smsdbDataSet.tb_CheckListDataTable();

                using (tb_CheckListTableAdapter adt = new tb_CheckListTableAdapter())
                {
                    adt.Fill(UnCheckTable, 0);

                }
                UnCheckTable[UnCheckTable.Rows.Count - 1].Delete();
                UpdateUnCheckList();
                UnCheckTable.AcceptChanges();
            }

            return UnCheckTable;
        }

        public smsdbDataSet.tb_CheckListDataTable GetCheckedListTable()
        {
            //smsdbDataSet.tb_SetDataTable tb = new smsdbDataSet.tb_SetDataTable();
            if (CheckTable == null)
            {
                using (QueriesTableAdapter adt = new QueriesTableAdapter())
                {
                    int a = adt.InsertCheckList();
                }
                CheckTable = new smsdbDataSet.tb_CheckListDataTable();

                using (tb_CheckListTableAdapter adt = new tb_CheckListTableAdapter())
                {
                    adt.Fill(CheckTable, 1);

                }
                CheckTable[CheckTable.Rows.Count - 1].Delete();
                UpdateCheckList();
                CheckTable.AcceptChanges();
            }

            return CheckTable;
        }
        public int UpdateCheckList()
        {

            int rowCount = 0;

            using (tb_CheckListTableAdapter adt = new tb_CheckListTableAdapter())
            {
                try
                {
                    rowCount = adt.Update(CheckTable);
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    rowCount = 0;
                }
            }
            CheckTable.AcceptChanges();
            return rowCount;


        }

        public int UpdateUnCheckList()
        {

            int rowCount = 0;

            using (tb_CheckListTableAdapter adt = new tb_CheckListTableAdapter())
            {
                try
                {
                    rowCount = adt.Update(UnCheckTable);
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    rowCount = 0;
                }
            }
            UnCheckTable.AcceptChanges();
            return rowCount;


        }
        public smsdbDataSet.tb_CheckListDataTable GetNeedCheckInfo()
        {
            smsdbDataSet.tb_CheckListDataTable checkListTable = null;

            using (tb_CheckListTableAdapter adt = new tb_CheckListTableAdapter())
            {
                checkListTable = adt.GetDataByState();
            }
            return checkListTable;
        }
        public int UpdateCheckStates(int id, int state, int checkState = 0)
        {
            int rowCount = 0;
            using (QueriesTableAdapter adt = new QueriesTableAdapter())
            {
                rowCount = adt.UpdateCheckState(state, checkState, id);


            }
            return rowCount;
        }
        public int DeleteChekcListByID(int id)
        {
            int rowCount = 0;
            using (QueriesTableAdapter adt = new QueriesTableAdapter())
            {
                rowCount = adt.DeleteCheckListByID(id);


            }
            return rowCount;
        }

        public int ClearChekcedList()
        {
            int rowCount = 0;
            using (QueriesTableAdapter adt = new QueriesTableAdapter())
            {
                rowCount = adt.ClearCheckList();


            }
            CheckTable = null;
            return rowCount;
        }
        public int ClearUnChekcedList()
        {
            int rowCount = 0;
            using (QueriesTableAdapter adt = new QueriesTableAdapter())
            {
                rowCount = adt.ClearUnCheckList();


            }
            UnCheckTable = null;
            return rowCount;
        }
    }
}
